const std = @import("std");

const DEFAULT_PORT = 27015;

pub fn main() !void {
    var client: std.net.Stream = undefined;
    {
        var server = std.net.StreamServer.init(.{});
        defer server.deinit();

        try server.listen(std.net.Address.initIp4(undefined, DEFAULT_PORT));
        client = (try server.accept()).stream;
    }
    defer client.close();
    var rec = std.ArrayList(u8).init(std.heap.page_allocator);
    defer rec.deinit();

    while (true) {
        var buf: [8]u8 = undefined;
        var r = try client.read(&buf);
        if(r == 0) break;
        try rec.appendSlice(buf[0..r]);
    }
    std.debug.print("Bytes recv: {d}\n", .{rec.items.len});
    var n = try client.write(rec.items);
    std.debug.print("Bytes Sent: {d}\n", .{n});
    try std.os.shutdown(client.handle, std.os.ShutdownHow.send);
}

test "simple test" {
    // var list = std.ArrayList(i32).init(std.testing.allocator);
    // defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
    // try list.append(42);
    // try std.testing.expectEqual(@as(i32, 42), list.pop());
}
